﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartContentModule.CssModel;
using SmartContentModule.Helpers;
using SmartContentModule.Extensions;

namespace SmartContentModule.VersionManagement
{
    internal class CssFileVersion
    {

        private static IDictionary<string, CssFileInfo> fileInfoTable = new Dictionary<string, CssFileInfo>();//contains CssFileInfos with key as url
        private static IDictionary<string, string> versionTable = new Dictionary<string, string>();//hash codes related to fileinfo urls

        /// <summary>
        /// Gets a CssFileInfo instance contained in hashtable related to version
        /// </summary>
        /// <param name="version">version of object to find</param>
        /// <returns>CssFileInfo instance</returns>
        public static CssFileInfo GetFileInfoForVersion(string version)
        {
            CssFileInfo info = null;
            if (versionTable.ContainsKey(version))
            {
                string contentUrl = versionTable[version].ToString();
                if (fileInfoTable.ContainsKey(contentUrl))
                {
                    info = fileInfoTable[contentUrl] as CssFileInfo;
                }
            }
            return info;
        }

        /// <summary>
        /// Adds new CssFileInfo
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="info">CssFileInfo instance related to url</param>
        public static void AddNewCssFileUrl(string url, CssFileInfo info)
        {
            //Clone, modify new, update old
            IDictionary<string, CssFileInfo> clone = fileInfoTable.Clone();
            if (clone.ContainsKey(url))
            {
                ExceptionHelper.HandleWarning("Url already exists in fileInfo table - " + url);
                return;
            }
            clone.Add(url, info);
            fileInfoTable = clone;
        }

        /// <summary>
        /// Add new version for url
        /// </summary>
        /// <param name="version">New version</param>
        /// <param name="url">url</param>
        public static void AddVersion(string version, string url)
        {
            //Clone, modify new, update old
            IDictionary<string, string> clone = versionTable.Clone();
            if (clone.ContainsKey(version))
            {
                ExceptionHelper.HandleWarning("Version already exists in version table - " + version);
                return;
            }
            clone.Add(version, url);
            versionTable = clone;
        }

        /// <summary>
        /// Gets url and fileinfo relations
        /// </summary>
        /// <returns>List of DictionaryEntries containing url-file relations</returns>
        public static KeyValuePair<string, CssFileInfo>[] GetFileInfoList()
        {
            lock (fileInfoTable)
            {
                KeyValuePair<string, CssFileInfo>[] list = new KeyValuePair<string, CssFileInfo>[fileInfoTable.Count];
                fileInfoTable.CopyTo(list, 0);
                return list;
            }
        }

        /// <summary>
        /// Gets a CssFileInfo instance contained in hashtable related to url
        /// </summary>
        /// <param name="url">url of object to find</param>
        /// <returns>CssFileInfo instance</returns>
        public static CssFileInfo GetFileInfoForUrl(string url)
        {
            if (fileInfoTable.ContainsKey(url))
                return fileInfoTable[url] as CssFileInfo;

            return null;
        }

        /// <summary>
        /// Gets temp file path for url.
        /// </summary>
        /// <param name="url">url is the url of file such as ~/Content/css/general.css</param>
        /// <returns>Returns a temp file path, this file contains a derived version of url</returns>
        public static string GetTempFileForFileInfo(string url)
        {
            if (fileInfoTable.ContainsKey(url))
            {
                CssFileInfo info = fileInfoTable[url] as CssFileInfo;
                return info == null ? String.Empty : info.TempPath;//log for null ?
            }
            else
                return String.Empty;
        }

        /// <summary>
        /// Gets version list for url
        /// </summary>
        /// <param name="url">url to check in versions table</param>
        /// <returns>Returns a list of versions</returns>
        public static List<string> GetVersionsUsingFileInfoUrl(string url)
        {
            List<string> versionList = new List<string>();
            KeyValuePair<string, string>[] entryList = new KeyValuePair<string, string>[versionTable.Count];
            versionTable.CopyTo(entryList, 0);
            foreach (KeyValuePair<string, string> pair in entryList)
            {
                if (pair.Value == url)
                    versionList.Add(pair.Key);
            }
            return versionList;
        }

    }
}
